package com.msb;

/**
 * @author lidexin
 * @create 2021-09-19 13:24
 */
//synchronized关键字对某个对象加锁
public class T {
    private int count =10;
    private Object o = new Object();

    public void m1() {
        synchronized(o) { //任何线程要执行下面的代码，必须要拿到o的锁
            count --;
            System.out.println(Thread.currentThread().getName() + "count = " + count);
        }
    }

    public void m2() {
        synchronized(this) {
            count --;
            System.out.println(Thread.currentThread().getName() + "count = " + count);
        }
    }

    public synchronized void m3() {//等同于在方法的代码块执行时 synchronized(this)
            count --;
            System.out.println(Thread.currentThread().getName() + "count = " + count);
    }
}
